import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_pdfviewer/pdfviewer.dart';
import 'package:get/get.dart';

class PDFViewer extends StatefulWidget {
  PDFViewer({Key? key, required String this.pdfaddress}) : super(key: key);
  final String? pdfaddress;

  @override
  State<PDFViewer> createState() => _PDFViewerState();
}

class _PDFViewerState extends State<PDFViewer> {
  late PdfViewerController _pdfViewerController;
  double zoomLevel = 1;

  @override
  void initState() {
    _pdfViewerController = PdfViewerController();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        floatingActionButton: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: SizedBox(
                width: 45.0,
                height: 45.0,
                child: FloatingActionButton(
                  heroTag: 'close',
                  onPressed: () {
                    Get.back();
                  },
                  backgroundColor: Theme.of(context).colorScheme.secondary,
                  child: const Icon(Icons.close_rounded),
                  tooltip: 'Close'.tr,
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: SizedBox(
                width: 45.0,
                height: 45.0,
                child: FloatingActionButton(
                  heroTag: 'zoomin',
                  child: Icon(
                    Icons.zoom_in,
                    color: Colors.white,
                  ),
                  onPressed: () {
                    if (zoomLevel <= 3) zoomLevel = zoomLevel + 0.5;
                    _pdfViewerController.zoomLevel = zoomLevel;
                  },
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: SizedBox(
                width: 45.0,
                height: 45.0,
                child: FloatingActionButton(
                  heroTag: 'zoomout',
                  child: Icon(
                    Icons.zoom_out,
                    color: Colors.white,
                  ),
                  onPressed: () {
                    if (zoomLevel >= 1.5) zoomLevel = zoomLevel - 0.5;
                    _pdfViewerController.zoomLevel = zoomLevel;
                  },
                ),
              ),
            ),
          ],
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.miniEndTop,
        body: Column(
          children: [
            Expanded(
                child: widget.pdfaddress!.startsWith('https://')
                    ? SfPdfViewer.network(widget.pdfaddress ?? '',
                        controller: _pdfViewerController,
                        interactionMode: PdfInteractionMode.pan,
                        canShowScrollStatus: true,
                        pageLayoutMode: PdfPageLayoutMode.single)
                    : SfPdfViewer.asset(
                        widget.pdfaddress ?? '',
                        interactionMode: PdfInteractionMode.pan,
                        canShowScrollStatus: true,
                        pageLayoutMode: PdfPageLayoutMode.continuous,
                        controller: _pdfViewerController,
                      )),
          ],
        ));
  }
}
